package com.trace.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trace.app.entity.Order;
import com.trace.app.entity.Product;
import com.trace.app.entity.ResponseResult;
import com.trace.app.entity.ShipAddress;
import com.trace.app.entity.consts.AppHttpCodeEnum;
import com.trace.app.entity.vo.OrderVo;
import com.trace.app.entity.vo.PageVo;
import com.trace.app.mapper.OrderMapper;
import com.trace.app.service.OrderService;
import com.trace.app.service.ProductService;
import com.trace.app.service.RawService;
import com.trace.app.service.ShipAddressService;
import com.trace.app.util.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (Order)表服务实现类
 *
 * @author lionkliu
 * @since 2024-04-22 18:18:44
 */
@Slf4j
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private ProductService productService;

    @Resource
    private ShipAddressService shipAddressService;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        Page<Order> page = page(new Page<>(pageNum, pageSize), queryWrapper);
        PageVo<Order> pageVo = new PageVo<>();
        pageVo.setResult(page.getRecords());
        pageVo.setTotal(page.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(Order order) {
        log.info("order:{}", order);
        return save(order) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(Order order) {
        LambdaUpdateWrapper<Order> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(Order::getId, order.getId());
        // TODO
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getOrderByUserId(Integer userId) {
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getUserId, userId)
                .orderByDesc(Order::getCreateTime);
        List<Order> orderList = list(queryWrapper);

        List<OrderVo> collect = orderList.stream().map(order -> {
            OrderVo orderVo = BeanCopyUtils.copyBean(order, OrderVo.class);

            LambdaQueryWrapper<Product> productLambdaQueryWrapper = new LambdaQueryWrapper<>();
            productLambdaQueryWrapper.eq(Product::getId, order.getProId());
            Product product = productService.getOne(productLambdaQueryWrapper);
            if (!Objects.isNull(product)) {
                orderVo.setProName(product.getName())
                        .setPrice(product.getPrice())
                        .setImg(product.getImg());
            }

            LambdaQueryWrapper<ShipAddress> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
            addressLambdaQueryWrapper.eq(ShipAddress::getId, order.getAddId());
            ShipAddress shipAddress = shipAddressService.getOne(addressLambdaQueryWrapper);
            if (!Objects.isNull(shipAddress)) {
                orderVo.setAddName(shipAddress.getName())
                        .setAddress(shipAddress.getAddress())
                        .setPhone(shipAddress.getPhone());
            }

            return orderVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }
}

